package ilvonen.ihstatistics.repository;

import ilvonen.ihstatistics.models.ConstantData;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class DBSeasons {
		//define table name
			private static final String TABLE_NAME = "seasons";
		//define tables column names
			private static final String KEY_ID = "_id";
			private static final String KEY_NAME = "name";
		//define create statement for all your tables. Create new file (class) for each table.
			private static final String CREATE_STATEMENT = "create table if not exists " + TABLE_NAME + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME + " text not null);";
		
		/*
		 * CLASS STATUS: READY?
		 */
		public DBSeasons(){}
		/*
		 * 
		 */
		public String create(){
			return CREATE_STATEMENT;
		}
		/*
		 * insert new values. this should be not used in this android program. seasons should be added throug web interface
		 */
		public long insert(String name, DBAdapter db){
			
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_NAME, name);
			
			return db.insertRecord(TABLE_NAME, initialValues);
			
		}
		/*
		 * delete records. seasons should not be deleted.
		 */
		public boolean delete(long rowId, DBAdapter db){
			return db.deleteRecord(TABLE_NAME, KEY_ID, rowId);
		}
		/*
		 * Get list of all seasons.
		 */
		public List<ConstantData> getAllSeasons(DBAdapter db){
			Cursor c;
			List<ConstantData> seasons = new ArrayList<ConstantData>();
			c = db.getAllRecords(TABLE_NAME, new String[]{KEY_ID,KEY_NAME});
			while(c.moveToNext()){
				ConstantData data = new ConstantData();
				data.set_id(c.getInt(0));
				data.set_name(c.getString(1));

				seasons.add(data);
			}
			return seasons;
		}
		/*
		 * Get one season identified by id
		 */
		public ConstantData getSeasonById(DBAdapter db, long ROW_ID){
			ConstantData s = null;
			Cursor c;
			c = db.getRecord(TABLE_NAME, new String[]{KEY_ID,KEY_NAME}, KEY_ID, ROW_ID);
			if(c != null){
				s = new ConstantData();
				c.moveToFirst();
				s.set_id(c.getInt(0));
				s.set_name(c.getString(1));
			}
			return s;
		}
}
